#!/usr/bin/env ruby1.9
# encoding: UTF-8

require './lib/hindo.rb'
require 'term/ansicolor'

describe TweetsSearch, "search" do

    it "Uses provided file name" do
        file = mock("File")

        file.should_receive(:new).once.with("file name").and_return(file)
        TweetsSearch.new("file name", file)
    end

    it "Returns tweets containing a given word" do
        file = mock("File")

        file.should_receive(:new).once.with("tweets.txt").and_return(file)
        file.should_receive(:readlines).once.and_return(["test one", "a second test"])
        TweetsSearch.new("tweets.txt", file).search("test").should == [red("test") + " one", "a second " + red("test")]
    end

    it "Should not return tweets without a given word" do
        file = mock("File")
        file.should_receive(:new).once.with("tweets.txt").and_return(file)
        file.should_receive(:readlines).once.and_return(["one", "a second"])
        TweetsSearch.new("tweets.txt", file).search("test").should == []
    end

    it "Should have a default constructor" do
        TweetsSearch.new()
    end

    it "Should highlight multiple occurences of a word" do
        file = mock "File"
        file.should_receive(:new).once.with("tweets.txt").and_return(file)
        file.should_receive(:readlines).once.and_return(["one plus one"])
        TweetsSearch.new("tweets.txt", file).search("one").should == [red("one") + " plus " + red("one")]
    end

end

describe TweetsLog, "search" do

    it "Should write to tweets files" do
        file = mock "File" 
        file.should_receive(:new).once.with("tweets.txt", "a").and_return(file)
        file.should_receive(:puts).once.with("120|person|a string")

        TweetsLog.new("tweets.txt", file).log Tweet.new(120, "person", "a string")
    end

    it "Should close file on close call" do
        file = mock("File")
        file.should_receive(:new).once.with("tweets.txt", "a").and_return(file)
        file.should_receive(:puts).once.with("a|b|monkey")
        file.should_receive(:close).once

        tweets = TweetsLog.new("tweets.txt", file)
        tweets.log Tweet.new("a","b","monkey")
        tweets.close
    end

    it "Uses provided file name" do
        file = mock("File")

        file.should_receive(:new).once.with("file name", "a").and_return(file)
        TweetsLog.new("file name", file)
    end

end
